﻿using System;
using System.Windows;
using System.Windows.Input;
using ConvertSourceToHtml.ClipboardUtilities;
using ConvertSourceToHtml.Conversion;
using ConvertSourceToHtml.Conversion.HtmlCodeElements;
using ConvertSourceToHtml.Properties;
using ConvertSourceToHtml.Troubleshooting;

namespace ConvertSourceToHtml
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.DragMove();
        }

        private void minimizeButton_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            e.Handled = true;
        }

        private void closeButton_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            e.Handled = true;
        }

        void settingsWindow_Closed(object sender, EventArgs e)
        {
            this.Show();
            this.Focus();
        }

        private void minimizeButton_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            this.WindowState = WindowState.Minimized;
        }

        private void closeButton_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            this.Close();
        }

        private void settingsButton_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            SettingsWindow settingsWindow = new SettingsWindow();
            settingsWindow.Owner = this;
            settingsWindow.Closed += new EventHandler(settingsWindow_Closed);
            this.Hide();
            settingsWindow.Show();
        }

        private void Convert()
        {
            string rtf;
            if (!ClipboardRtfReader.Read(out rtf))
            {
                MessageBox.Show("Could not read from the clipboard.  Another application may have the clipboard locked.",
                    "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }
            if (rtf == null)
            {
                MessageBox.Show("There is no RTF data on the clipboard.", "Error", MessageBoxButton.OK,
                    MessageBoxImage.Error);
                return;
            }

            ColorableItemStyle plainTextStyle = new ColorableItemStyle(
                new HtmlColor(0, 0, 0),
                new HtmlColor(Settings.Default.BackgroundColorR, Settings.Default.BackgroundColorG, Settings.Default.BackgroundColorB),
                false);
            ColorableItemStyle lineNumberStyle = new ColorableItemStyle(
                new HtmlColor(Settings.Default.LineNumberColorR, Settings.Default.LineNumberColorG, Settings.Default.LineNumberColorB),
                new HtmlColor(Settings.Default.BackgroundColorR, Settings.Default.BackgroundColorG, Settings.Default.BackgroundColorB),
                false, plainTextStyle);
            lineNumberStyle.Append(Settings.Default.LineNumberStyle);

            Copier copier = new Copier(Settings.Default, plainTextStyle, lineNumberStyle);
            string html = copier.CopyAsHtml(rtf);

            if (Settings.Default.SaveScenarios)
            {
                Scenario scenario = new Scenario()
                {
                    Settings = Settings.Default,
                    PlainTextStyle = plainTextStyle,
                    Rtf = rtf,
                    Html = html
                };
                Scenario.SaveScenario(scenario);
            }

            if (!ClipboardHtmlWriter.Write(html, Settings.Default.RestartVirtualMachineAdditions))
            {
                MessageBox.Show("Could not write to the clipboard. Another application may have the clipboard locked.",
                    "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        private void convertButton_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            Convert();
        }
    }
}